package org.campuscontent.ui.multipanel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ResourceBundle;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.campuscontent.config.languages.LanguageListener;
import org.campuscontent.config.languages.LanguageManager;

public class MultiStylePanel extends JPanel implements LanguageListener,  AbstractUnit
{
    public static String id = "multi";
    
    public int getId()
    {
        return 1;
    }

    public String id()
    {
        return id;
    }
   
    public String label()
    {
        return LanguageManager.str( AbstractUnit.class, id + ".label" );
    }
    
    public JComponent parent()
    {
        return this;
    }

    public MultiStylePanel()
    {
        init();
    }
    
    protected void init()
    {
        setLayout( new BorderLayout() );
        JPanel jpContainer = new JPanel();
        jpContainer.setLayout( new BoxLayout( jpContainer, BoxLayout.X_AXIS ) );
        
        add( new Activator(), BorderLayout.NORTH );
        add( jpContainer );
        
        jpTextArea     = new JPanel();
        jpRadioButtons = new JPanel();
        jpCheckButtons = new JPanel();
        
        jpTextArea    .setBorder( new TitledBorder( LanguageManager.str( AbstractUnit.class, id + ".caption.textarea" ) ) );
        jpRadioButtons.setBorder( new TitledBorder( LanguageManager.str( AbstractUnit.class, id + ".caption.radios"   ) ) );
        jpCheckButtons.setBorder( new TitledBorder( LanguageManager.str( AbstractUnit.class, id + ".caption.checks"   ) ) );
        
        jpContainer.add( jpTextArea     );
        jpContainer.add( jpRadioButtons );
        jpContainer.add( jpCheckButtons );
        
        initTextArea   ();
        initRadiosPanel();
        initChecksPanel();
        
        initTextAreaLabels();
        initRadiosLabels  ();
        initCheckLabels   ();
    }
    
    public void update( ResourceBundle resources )
    {
        jpTextArea    .setBorder( new TitledBorder( LanguageManager.str( AbstractUnit.class, id + ".caption.textarea" ) ) );
        jpRadioButtons.setBorder( new TitledBorder( LanguageManager.str( AbstractUnit.class, id + ".caption.radios"   ) ) );
        jpCheckButtons.setBorder( new TitledBorder( LanguageManager.str( AbstractUnit.class, id + ".caption.checks"   ) ) );
    }
    
    protected void initTextArea()
    {
        textarea   = new JTextArea();
        
        // jpTextArea
        jpTextArea.setLayout( new BorderLayout() );
        jpTextArea.add( new JScrollPane( textarea ), BorderLayout.CENTER );
    }
    
    protected void initTextAreaLabels()
    {
    }
    
    protected void initRadiosPanel()
    {
         jrRB_first     = new JRadioButton();
         jrRB_second    = new JRadioButton();
         jrRB_third     = new JRadioButton();
         jrRB_fourth    = new JRadioButton();
         teRB_first     = new JTextField  ();
         teRB_second    = new JTextField  ();
         teRB_third     = new JTextField  ();
         teRB_fourth    = new JTextField  ();
         
         // jpRadioButtons
         jpRadioButtons.setLayout( new BorderLayout() );
         
         JPanel composite = new JPanel();
         composite.setLayout( new BoxLayout( composite, BoxLayout.Y_AXIS ) );

         composite.add( rowOf( jrRB_first , teRB_first  ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );
         composite.add( rowOf( jrRB_second, teRB_second ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );
         composite.add( rowOf( jrRB_third , teRB_third  ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );
         composite.add( rowOf( jrRB_fourth, teRB_fourth ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );
         
         jpRadioButtons.add( composite, BorderLayout.NORTH );
    }
    
    protected void initRadiosLabels()
    {
    }
    
    protected void initChecksPanel()
    {
        jcCB_first     = new JCheckBox ();
        jcCB_second    = new JCheckBox ();
        jcCB_third     = new JCheckBox ();
        jcCB_fourth    = new JCheckBox ();
        teCB_first     = new JTextField();
        teCB_second    = new JTextField();
        teCB_third     = new JTextField();
        teCB_fourth    = new JTextField();
        
        // jpCheckButtons
        jpCheckButtons.setLayout( new BorderLayout() );
        
        JPanel composite = new JPanel();
        composite.setLayout( new BoxLayout( composite, BoxLayout.Y_AXIS ) );

        composite.add( rowOf( jcCB_first  , teCB_first  ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) ); 
        composite.add( rowOf( jcCB_second , teCB_second ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );
        composite.add( rowOf( jcCB_third  , teCB_third  ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );
        composite.add( rowOf( jcCB_fourth , teCB_fourth ) );composite.add( rowOf( Box.createVerticalStrut( 2 ), Box.createVerticalStrut( 2 ) ) );

        jpCheckButtons.add( composite, BorderLayout.NORTH );
    }
    
    protected void initCheckLabels()
    {
    }
    
    protected JPanel rowOf( Component left, Component right )
    {
        JPanel jp = new JPanel();
        jp.setLayout( new BoxLayout( jp, BoxLayout.X_AXIS ) );
        jp.add( left  );
        jp.add( right );
        
        return jp;
    }
    
    private static void test()
    {
        JFrame jf = new JFrame();
        jf.setPreferredSize( new Dimension( 600, 400 ) );
        jf.add( new MultiStylePanel() );
        jf.pack();
        jf.setVisible( true );
    }
    
    public static void main( String[] args )
    {
        test();
    }
    
    protected JPanel jpTextArea    ;
    protected JPanel jpRadioButtons;
    protected JPanel jpCheckButtons;
    
    // Text Area Block
    protected JTextArea textarea  ;
    
    // RadioButtons Block
    protected JRadioButton jrRB_first ;
    protected JRadioButton jrRB_second;
    protected JRadioButton jrRB_third ;
    protected JRadioButton jrRB_fourth;
    protected JTextField   teRB_first ;
    protected JTextField   teRB_second;
    protected JTextField   teRB_third ;
    protected JTextField   teRB_fourth;
    
    // CheckButtons Block
    protected JCheckBox  jcCB_first ;
    protected JCheckBox  jcCB_second;
    protected JCheckBox  jcCB_third ;
    protected JCheckBox  jcCB_fourth;
    protected JTextField teCB_first ;
    protected JTextField teCB_second;
    protected JTextField teCB_third ;
    protected JTextField teCB_fourth;
    
    public class Activator extends JPanel implements ChangeListener
    {
        public Activator()
        {
            setLayout( new FlowLayout( FlowLayout.LEFT ) );
            
            Class cl = getClass();
            
            ImageIcon iiTA = new ImageIcon( cl.getResource( "/org/campuscontent/images/multi/textarea.png" ) );
            ImageIcon iiRA = new ImageIcon( cl.getResource( "/org/campuscontent/images/multi/radios.png"   ) );
            ImageIcon iiCK = new ImageIcon( cl.getResource( "/org/campuscontent/images/multi/checks.png"   ) );

            textarea = new JToggleButton( iiTA, true );
            radios   = new JToggleButton( iiRA, true );
            checks   = new JToggleButton( iiCK, true );
            
            textarea.addChangeListener( this );
            radios  .addChangeListener( this );
            checks  .addChangeListener( this );
            
            add( textarea );
            add( radios   );
            add( checks   );
        }
        
        public void stateChanged( ChangeEvent e )
        {
            Object obj = e.getSource();
            if ( obj == textarea )
            {
                jpTextArea.setVisible( !jpTextArea.isVisible() );    
            }
            else if ( obj == radios )
            {
                jpRadioButtons.setVisible( !jpRadioButtons.isVisible() );    
            }
            else if ( obj == checks )
            {
                jpCheckButtons.setVisible( !jpCheckButtons.isVisible() );    
            }
        }
        
        public JToggleButton textarea;
        public JToggleButton radios  ;
        public JToggleButton checks  ;
    }
}
